package com.wanlink.mall.app;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanlink.mall.R;
import com.wanlink.mall.app.ui.adapter.MainFragPagerAdapter;
import com.wanlink.mall.app.ui.fragment.BuyFragment;
import com.wanlink.mall.app.ui.fragment.CarFragment;
import com.wanlink.mall.app.ui.fragment.CommunityFragment;
import com.wanlink.mall.app.ui.fragment.HomeFragment;
import com.wanlink.mall.app.ui.fragment.UserFragment;
import com.wanlink.mall.base.BaseActivity;
import com.wanlink.mall.base.BasePresenter;
import com.wanlink.mall.util.PermissionsUtil;
import com.wanlink.mall.util.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    private static final int TIME_INTERVAL = 2000; // # milliseconds, desired time passed between two back presses.
    @BindView(R.id.iv_home)
    ImageView ivHome;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.iv_community)
    ImageView ivCommunity;
    @BindView(R.id.tv_community)
    TextView tvCommunity;
    @BindView(R.id.iv_buy)
    ImageView ivBuy;
    @BindView(R.id.tv_buy)
    TextView tvBuy;
    @BindView(R.id.iv_car)
    ImageView ivCar;
    @BindView(R.id.tv_car)
    TextView tvCar;
    @BindView(R.id.iv_user)
    ImageView ivUser;
    @BindView(R.id.tv_user)
    TextView tvUser;

    private List<Fragment> mFragments = new ArrayList<>();


    @BindView(R.id.viewpager)
    ViewPager viewPager;


    private HomeFragment homeFragment;
    private CommunityFragment communityFragment;
    private BuyFragment buyFragment;
    private CarFragment carFragment;
    private UserFragment userFragment;

    private long mBackPressed;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        homeFragment = HomeFragment.newInstance();
        communityFragment = CommunityFragment.newInstance();
        buyFragment = BuyFragment.newInstance();
        carFragment = CarFragment.newInstance();
        userFragment = UserFragment.newInstance();

        mFragments.add(homeFragment);
        mFragments.add(communityFragment);
        mFragments.add(buyFragment);
        mFragments.add(carFragment);
        mFragments.add(userFragment);
        viewPager.setAdapter(new MainFragPagerAdapter(getSupportFragmentManager(), mFragments));
        viewPager.setCurrentItem(0, false);
        viewPager.setOffscreenPageLimit(5);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTabColor(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void initData() {
//        boolean isAuto = getIntent().getBooleanExtra(LoginActivity.ISAUTO, true);
//        if (isAuto) {
        PermissionsUtil.requestPermissions(this, PermissionsUtil.allPermissions);//自动登录需在MainActivity中校验校验权限
//        }
    }


    @Override
    public boolean needTitleBar() {
        return false;
    }

    private void setTabColor(int tabIndex) {
        ivHome.setImageResource(R.mipmap.tab_home_normal);
        tvHome.setTextColor(UIUtils.getColor(R.color.tab_normal));
        ivCommunity.setImageResource(R.mipmap.tab_community_normal);
        tvCommunity.setTextColor(UIUtils.getColor(R.color.tab_normal));
        ivBuy.setImageResource(R.mipmap.tab_buy_normal);
        tvBuy.setTextColor(UIUtils.getColor(R.color.tab_normal));
        ivCar.setImageResource(R.mipmap.tab_car_normal);
        tvCar.setTextColor(UIUtils.getColor(R.color.tab_normal));
        ivUser.setImageResource(R.mipmap.tab_user_normal);
        tvUser.setTextColor(UIUtils.getColor(R.color.tab_normal));

        switch (tabIndex) {
            case 0:
                ivHome.setImageResource(R.mipmap.tab_home_active);
                tvHome.setTextColor(UIUtils.getColor(R.color.tab_active));
                break;
            case 1:
                ivCommunity.setImageResource(R.mipmap.tab_community_active);
                tvCommunity.setTextColor(UIUtils.getColor(R.color.tab_active));
                break;
            case 2:
                ivBuy.setImageResource(R.mipmap.tab_buy_active);
                tvBuy.setTextColor(UIUtils.getColor(R.color.tab_active));
                break;
            case 3:
                ivCar.setImageResource(R.mipmap.tab_car_active);
                tvCar.setTextColor(UIUtils.getColor(R.color.tab_active));
                break;
            case 4:
                ivUser.setImageResource(R.mipmap.tab_user_active);
                tvUser.setTextColor(UIUtils.getColor(R.color.tab_active));
                break;
            default:
        }

    }


    @Override
    public void onBackPressed() {
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
            AppManager.getAppManager().AppExit(mContext, true);
            super.onBackPressed();
            return;
        } else {
            showToast("再按一次退出应用");
        }
        mBackPressed = System.currentTimeMillis();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.ll_home, R.id.ll_community, R.id.ll_buy, R.id.ll_car, R.id.ll_user})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_home:
                viewPager.setCurrentItem(0, false);
                setTabColor(0);
                break;
            case R.id.ll_community:
                viewPager.setCurrentItem(1, false);
                setTabColor(1);
                break;
            case R.id.ll_buy:
                viewPager.setCurrentItem(2, false);
                setTabColor(2);
                break;
            case R.id.ll_car:
                viewPager.setCurrentItem(3, false);
                setTabColor(3);
                break;
            case R.id.ll_user:
                viewPager.setCurrentItem(4, false);
                setTabColor(4);
                break;
        }
    }
}
